package com.wangyf.thread2;


import java.util.concurrent.CountDownLatch;

/**
 * 测试缓存行
 * @author wangyf
 * @date 2021/7/20 21:00
 */
public class T09_CacheLine {

    public static long COUNT = 1_0000_0000;
    private static class T {
        // 这里注释和不注释 观察时间
        private long p1, p2, p3, p4, p5, p6, p7;
        // 这里如果不使用volatile修饰，无法查看到时间的不一致！！！
        public volatile long x = 0;
        // 这里注释和不注释 观察时间
        private long p9, p10, p11, p12, p13, p14, p15;
    }
    public static T[] arr = new T[2];

    static {
        arr[0] = new T();
        arr[1] = new T();
    }
    public static void main(String[] args) throws Exception {
        CountDownLatch latch = new CountDownLatch(2);
        Thread t1 = new Thread(() -> {
            for (long i = 0; i < COUNT; i++) {
                arr[0].x = i;
            }
            latch.countDown();
        });
        Thread t2 = new Thread(() -> {
            for (long i = 0; i < COUNT; i++) {
                arr[1].x = i;
            }
            latch.countDown();
        });

        final long start = System.nanoTime();
        t1.start();
        t2.start();
        latch.await();
        System.out.println((System.nanoTime() - start) / 100_0000);
    }
}
